#include "SIX_Scale9PopWnd.h"

SIX_Scale9PopWnd::SIX_Scale9PopWnd()
{
}

SIX_Scale9PopWnd::~SIX_Scale9PopWnd()
{
}

SIX_Scale9PopWnd *SIX_Scale9PopWnd::Create(float x, float y, const ccColor4B& color, float width, float height, float shakeOffset, MOVABLE_TYPE showType, MOVABLE_TYPE hideType, bool bSeqExec)
{
	CCScene *pScene = CCDirector::sharedDirector()->getRunningScene();
	if (!pScene)
		return 0;

	SIX_Scale9PopWnd *pPopWnd = new SIX_Scale9PopWnd();
	if (pPopWnd && pPopWnd->init())
	{
		pPopWnd->initWithColor(color, width, height);
		pPopWnd->setPosition(x, y);
		pPopWnd->setRedrawBorder(false);
		pPopWnd->m_ForbiddenTouch = true;
		pPopWnd->m_bCloseThenCleanup = false;

		pPopWnd->setShakeOffset(shakeOffset);
		pPopWnd->setShowType(showType);
		pPopWnd->setHideType(hideType);
		pPopWnd->setSeqExec(bSeqExec);
	}
	return pPopWnd;
}

bool SIX_Scale9PopWnd::init()
{
	if (!SIX_EffectPopWnd::init())
		return false;

	m_pBackground = NULL;
	topLeft = NULL;
	top = NULL;
	topRight = NULL;
	left = NULL;
	centre = NULL;
	right = NULL;
	bottomLeft = NULL;
	bottom = NULL;
	bottomRight = NULL;

	m_bTopTile = false;
	m_bBottomTile = false;
	m_bLeftTile = false;
	m_bRightTile = false;

	m_BGExtrudeSize.setSize(0.0f, 0.0f);
	m_ptBGPosition.setPoint(0.0f, 0.0f);
	return true;
}

void SIX_Scale9PopWnd::updateBGPositions()
{
	if (!topLeft || !topRight || !bottomLeft || !bottomRight)
		return;

	const CCSize size = this->getBGExtrudeSize();

	float sizableWidth_T = size.width - topLeft->getContentSize().width - topRight->getContentSize().width;
	float sizableHeight_L = size.height - topLeft->getContentSize().height - bottomLeft->getContentSize().height;
	float sizableWidth_C = size.width - left->getContentSize().width - right->getContentSize().width;
	float sizableHeight_C = size.height - top->getContentSize().height - bottom->getContentSize().height;
	float sizableWidth_B = size.width - bottomLeft->getContentSize().width - bottomRight->getContentSize().width;
	float sizableHeight_R = size.height - topRight->getContentSize().height - bottomRight->getContentSize().height;

	float horizontalScale = sizableWidth_C / centre->getContentSize().width;
	float horizontalScale_T = sizableWidth_T / top->getContentSize().width;
	float horizontalScale_B = sizableWidth_B / bottom->getContentSize().width;
	float verticalScale = sizableHeight_C / centre->getContentSize().height;
	float verticalScale_L = sizableHeight_L / left->getContentSize().height;
	float verticalScale_R = sizableHeight_R / right->getContentSize().height;

	centre->setScaleX(horizontalScale);
	centre->setScaleY(verticalScale);

	float rescaledWidth = centre->getContentSize().width * horizontalScale;
	float rescaledHeight = centre->getContentSize().height * verticalScale;

	float leftWidth = bottomLeft->getContentSize().width;
	float bottomHeight = bottomLeft->getContentSize().height;

	bottomLeft->setAnchorPoint(ccp(0, 0));
	bottomRight->setAnchorPoint(ccp(0, 0));
	topLeft->setAnchorPoint(ccp(0, 0));
	topRight->setAnchorPoint(ccp(0, 0));
	left->setAnchorPoint(ccp(0, 0));
	right->setAnchorPoint(ccp(0, 0));
	top->setAnchorPoint(ccp(0, 0));
	bottom->setAnchorPoint(ccp(0, 0));
	centre->setAnchorPoint(ccp(0, 0));

	// Position corners
	bottomLeft->setPosition(ccp(0, 0));
	bottomRight->setPosition(ccp(leftWidth + rescaledWidth, 0));
	topLeft->setPosition(ccp(0, bottomHeight + rescaledHeight));
	topRight->setPosition(ccp(leftWidth + rescaledWidth, bottomHeight + rescaledHeight));

	// Scale and position borders
	left->setPosition(ccp(0, bottomHeight));
	if (m_bLeftTile)
		startTitle(left, false, sizableHeight_L, left->getContentSize().height);
	else
		left->setScaleY(verticalScale_L);

	right->setPosition(ccp(leftWidth + rescaledWidth, bottomHeight));
	if (m_bRightTile)
		startTitle(right, false, sizableHeight_R, right->getContentSize().height);
	else
		right->setScaleY(verticalScale_R);

	bottom->setPosition(ccp(leftWidth, 0));
	if (m_bBottomTile)
		startTitle(bottom, true, sizableWidth_B, bottom->getContentSize().width);
	else
		bottom->setScaleX(horizontalScale_B);

	top->setPosition(ccp(leftWidth, bottomHeight + rescaledHeight));
	if (m_bTopTile)
		startTitle(top, true, sizableWidth_T, top->getContentSize().width);
	else
		top->setScaleX(horizontalScale_T);

	// Position centre
	centre->setPosition(ccp(leftWidth, bottomHeight));
}

void SIX_Scale9PopWnd::backgroundWithSpriteFrameName(const char *AllFile, const char*tl, const char*tc, const char*tr, const char*cl, const char*cc, const char*cr, const char*bl, const char*bc, const char*br)
{
	m_pBackground = CCSpriteBatchNode::create(AllFile);
	addChild(m_pBackground);

	centre = CCSprite::createWithSpriteFrameName(cc);
	m_pBackground->addChild(centre, 0);

	top = CCSprite::createWithSpriteFrameName(tc);
	m_pBackground->addChild(top, 1);
	left = CCSprite::createWithSpriteFrameName(cl);
	m_pBackground->addChild(left, 1);
	right = CCSprite::createWithSpriteFrameName(cr);
	m_pBackground->addChild(right, 1);
	bottom = CCSprite::createWithSpriteFrameName(bc);
	m_pBackground->addChild(bottom, 1);

	topLeft = CCSprite::createWithSpriteFrameName(tl);
	m_pBackground->addChild(topLeft, 2);
	topRight = CCSprite::createWithSpriteFrameName(tr);
	m_pBackground->addChild(topRight, 2);
	bottomLeft = CCSprite::createWithSpriteFrameName(bl);
	m_pBackground->addChild(bottomLeft, 2);
	bottomRight = CCSprite::createWithSpriteFrameName(br);
	m_pBackground->addChild(bottomRight, 2);

	if (m_BGExtrudeSize.equals(CCSizeZero))
		setBGExtrudeSize(m_tContentSize);
	else
		updateBGPositions();
}

const bool &SIX_Scale9PopWnd::getTopTile()
{
	return m_bTopTile;
}

void SIX_Scale9PopWnd::setTopTile(const bool &bIsTitle)
{
	m_bTopTile = bIsTitle;
	updateBGPositions();
}

const bool &SIX_Scale9PopWnd::getBottomTile()
{
	return m_bBottomTile;
}

void SIX_Scale9PopWnd::setBottomTile(const bool &bIsTitle)
{
	m_bBottomTile = bIsTitle;
	updateBGPositions();
}

const bool &SIX_Scale9PopWnd::getLeftTile()
{
	return m_bLeftTile;
}

void SIX_Scale9PopWnd::setLeftTile(const bool &bIsTitle)
{
	m_bLeftTile = bIsTitle;
	updateBGPositions();
}

const bool &SIX_Scale9PopWnd::getRightTile()
{
	return m_bRightTile;
}

void SIX_Scale9PopWnd::setRightTile(const bool &bIsTitle)
{
	m_bRightTile = bIsTitle;
	updateBGPositions();
}

const CCSize &SIX_Scale9PopWnd::getBGExtrudeSize()
{
	return m_BGExtrudeSize;
}

void SIX_Scale9PopWnd::setBGExtrudeSize(const CCSize &size)
{
	m_BGExtrudeSize = size;
	updateBGPositions();
}

const CCPoint &SIX_Scale9PopWnd::getBGPosition()
{
	return m_ptBGPosition;
}

void SIX_Scale9PopWnd::setBGPosition(const CCPoint &pt)
{
	m_ptBGPosition = pt;
	if (m_pBackground)
		m_pBackground->setPosition(m_ptBGPosition);
}

void SIX_Scale9PopWnd::startTitle(CCSprite *pNode, bool bX, float fEnd, float fStep)
{
	pNode->removeAllChildrenWithCleanup(true);
	for (float fOffset = fStep; fOffset < fEnd; fOffset += fStep)
	{
		CCSprite *pCopy = CCSprite::createWithSpriteFrame(pNode->mDisplayFrame);
		pCopy->ignoreAnchorPointForPosition(true);
		pCopy->setOpacity(255);
		pNode->addChild(pCopy);

		bX ? pCopy->setPositionX(fOffset) : pCopy->setPositionY(fOffset);
		//SIXLog("startTitle.[%f,%f,%f]", fStep, fEnd, fOffset);
	}
}